Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
MongooseError: Query was already executed: Todo.updateOne({ _id: new ObjectId("612df063a8f

I updated mongoose to latest version (6.0.2) and now I'm recieving this error and crush the application whenever .updateOne() is executed. But object update inside the database. My code:

async(req,res) => {
    await Todo.updateOne(
        {_id : req.params.id},
        {
            $set : {
                status : "inactive"
            }
        },
        (updateError) => {
            // if updateError exist
            if (updateError) {
                // print error
                printError(updateError);

                // response the error
                res.status(500).json({
                    error : "There was a Server Side Error!"
                });
            } else {
                // if error dose not exist
                // print message
                console.log("|===> ๐Ÿ—‚๏ธ  Data Was Updated successfully! ๐Ÿ—‚๏ธ <====|\n");
                // response success
                res.json({
                    message : "Todo Was Update successfully!"
                });
            }
        });
    }
about 4 years ago ยท Juan Pablo Isaza
3 answers
Answer question

0

Try to add .clone() to de updateOne function

async(req,res) => {
    await Todo.updateOne(
         {_id : req.params.id},
         {
              $set : {
                    status : "inactive"
              }
         },
         (updateError) => {
              // if updateError exist
              if (updateError) {
                    // print error
                    printError(updateError);

                    // response the error
                    res.status(500).json({
                         error : "There was a Server Side Error!"
                    });
              } else {
                    // if error dose not exist
                    // print message
                    console.log("|===> ๐Ÿ—‚๏ธ  Data Was Updated successfully! ๐Ÿ—‚๏ธ <====|\n");
                    // response success
                    res.json({
                         message : "Todo Was Update successfully!"
                    });
              }
         }).clone();
    }

Latest version of mongoose not duplicate execution

https://mongoosejs.com/docs/migrating_to_6.html#duplicate-query-execution

about 4 years ago ยท Juan Pablo Isaza Report

0

Since you are using async syntax, put your code in try/catch blok:

async (req, res) => {
  try {
    await Todo.updateOne({ _id: req.params.id }, { $set: { status: "inactive"}});
    res.status(200).json({message: "Todo Was Update successfully!"});
  } catch (error) {
    res.status(500).json({error:'There was a Server Side Error!'})
  }
}

about 4 years ago ยท Juan Pablo Isaza Report

0

Your async/await should look like this:

async (req,res) => {
  try {
      const result = await Todo.updateOne(
          {_id : req.params.id},
          {
              $set : {
                  status : "inactive"
              }
          },
          );
      }
      console.log('success', result)
      res.json({message: "Todo Was Update successfully!", result })
  } catch (err) {
     console.log('error', err)
     res.status(500).json({error:'There was a Server Side Error!'})
  }
}

about 4 years ago ยท Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!